package cn.huanba.controller;

import cn.huanba.client.DictService;
import cn.huanba.client.ProductService;
import cn.huanba.client.UserService;
import cn.huanba.dto.Result;
import cn.huanba.entity.Dock;
import cn.huanba.entity.Picture;
import cn.huanba.entity.Product;
import cn.huanba.entity.User;
import cn.huanba.utils.FileUtils;
import cn.huanba.utils.OssUtils;
import cn.huanba.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2021-01-07 17:23:27
 */
@RestController
@RequestMapping("product")
@Api(value = "玩具产品接口", tags = "玩具产品接口", description = "玩具产品模块控制类")

public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Resource
    private UserService userService;
    @Resource
    private DictService dictService;
    @Resource
    private OssUtils ossUtils;

    /**
     * 主页数据
     *
     * @param
     * @return 类型集合 与 玩具集合
     */
    @RequestMapping("home")
    @ApiOperation(value = "主页", notes = "主页")
    public Result home() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("typeList", dictService.queryTypeAll());//类型集合
        map.put("productList", productService.queryAllByproductName(""));//商品集合
        return ResultUtils.returnDataSuccess(map);
    }


    /**
     * 根据商品名称 模糊查询
     *
     * @param productName 玩具名称
     * @return 集合
     */
    @RequestMapping("getProductList")
    @ApiOperation(value = "获取玩具集合", notes = "通过玩具名称玩具集合")
    @ApiImplicitParam(name = "productName", value = "玩具名称", paramType = "query", required = false, dataType = "string")
    public Result getProductList(String productName) {
        if (null == productName) {
            productName = "";
        }
        return ResultUtils.returnDataSuccess(productService.queryAllByproductName(productName));
    }

    /**
     * 通过类别id查询商品集合
     *
     * @param tid 类别id
     * @return 商品集合
     */
    @RequestMapping("getProductListByTid")
    @ApiOperation(value = "通过类别id查询商品集合", notes = "通过类别id查询商品集合")
    @ApiImplicitParam(name = "tid", value = "类别编号", paramType = "query", required = true, dataType = "Integer")
    public Result getProductListByTid(Integer tid) {
        return ResultUtils.returnDataSuccess(this.productService.getProductListByTid(tid));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    @ApiOperation(value = "获取玩具对象", notes = "通过玩具编号获取玩具对象")
    @ApiImplicitParam(name = "id", value = "玩具编号", paramType = "query", required = true, dataType = "Integer")

    public Result selectOne(Integer id) {
        return ResultUtils.returnDataSuccess(this.productService.queryById(id));
    }

    /**
     * 发布商品
     *
     * @param product 类别编号tid 产品名称productname 产品描述describe 图片集合pictureList
     * @return msg
     */
    @RequestMapping("addProduct")
    @ApiOperation(value = "发布商品", notes = "发布商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid", value = "类别编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "productname", value = "产品名称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "describe", value = "产品描述", required = true, dataType = "string"),
            @ApiImplicitParam(name = "attaches", value = "图片集合", required = true, dataType = "File")
    })
    public Result addProduct(Product product, HttpServletRequest request,
                             @RequestParam("attaches") MultipartFile[] attaches) {
        String token = request.getHeader("token");
        User user = userService.getUserByToken(token);
        if (user == null) {
            return ResultUtils.returnFail("发布失败,请重新登录");
        }
        String newFilename = "";
        if (attaches != null) {
            System.out.println("图:" + attaches.length);
            for (MultipartFile attach : attaches) {
                File upFile = FileUtils.MultipartFileToFile(attach);
                newFilename = UUID.randomUUID().toString().replace("-", "");
                ossUtils.fileUpload("products/", newFilename, upFile);
                upFile.delete();
                Picture picture = new Picture();
                picture.setPath("products/" + newFilename);
                product.getPictureList().add(picture);
            }
        } else {
            System.out.println("图片为null");
        }
        product.setUid(user.getUid());
        product.setCreatedate(new Date());
        int rows = this.productService.insert(product);
        // int rows = 1;
        return rows > 0 ? ResultUtils.returnSuccess() : ResultUtils.returnFail();
    }

    @GetMapping("getProductListByUser")
    @ApiOperation(value = "根据用户查询发布商品", notes = "根据用户查询发布商品")
    @ApiImplicitParam(name = "uid", value = "用户id", paramType = "query", required = true, dataType = "Integer")
    public Result getProductListByUser(Integer uid) {
        return ResultUtils.returnDataSuccess(this.productService.getProductListByUser(uid));
    }

    /**
     * 发送交易请求
     *
     * @param fpid
     * @param jpid
     * @param message
     * @return
     */
    @GetMapping("postDock")
    @ApiOperation(value = "发送交易请求", notes = "发送交易请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fpid", value = "发起方商品id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "jpid", value = "交易方商品id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "message", value = "留言信息", required = false, dataType = "string")
    })
    public Result postDock(Integer fpid, Integer jpid, String message) {
        Dock dock = new Dock();
        dock.setFpid(fpid);
        dock.setJpid(jpid);
        dock.setCreatedate(new Date());
        return (this.productService.saveDock(dock) > 0) ?
                ResultUtils.returnSuccess() :
                ResultUtils.returnFail();
    }

    /**
     * 查看谁想换我的宝贝
     *
     * @param request
     * @return
     */

    @GetMapping("findDockByFuid")
    @ApiOperation(value = "查看谁想换我的宝贝", notes = "查看谁想换我的宝贝")
    public Result findDockByFuid(HttpServletRequest request) {
        String token = request.getHeader("token");
        User user = userService.getUserByToken(token);
        if (user == null) {
            return ResultUtils.returnFail("查看失败,请重新登录");
        }

        return ResultUtils.returnDataSuccess(productService.getProductAndDock(user.getUid()));
    }


}